package com.ewing.core.mppaysdk;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class PayCommonUtil {
  @SuppressWarnings("unused")
  private static Logger log = LoggerFactory.getLogger(PayCommonUtil.class);

  public static String CreateNoncestr(int length) {
    String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    String res = "";
    for (int i = 0; i < length; i++) {
      Random rd = new Random();
      res += chars.indexOf(rd.nextInt(chars.length() - 1));
    }
    return res;
  }

  public static String CreateNoncestr() {
    String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    String res = "";
    for (int i = 0; i < 16; i++) {
      Random rd = new Random();
      res += chars.charAt(rd.nextInt(chars.length() - 1));
    }
    return res;
  }

  /**
   * @author Joeson
   * @date 2014-12-5下午2:29:34
   * @Description：sign签名
   * @param characterEncoding 编码格式
   * @param parameters 请求参数
   * @return
   */
  public static String createSign(String characterEncoding, SortedMap<Object, Object> parameters) {
    StringBuffer sb = new StringBuffer();
    Set<Entry<Object, Object>> es = parameters.entrySet();
    Iterator<Entry<Object, Object>> it = es.iterator();
    while (it.hasNext()) {
      Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
      String k = (String) entry.getKey();
      Object v = entry.getValue();
      if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
        sb.append(k + "=" + v + "&");
      }
    }
    sb.append("key=" + ConfigUtil.API_KEY);
    String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
    return sign;
  }

  /**
   * @author Joeson
   * @date 2014-12-5下午2:32:05
   * @Description：将请求参数转换为xml格式的string
   * @param parameters 请求参数
   * @return
   */
  public static String getRequestXml(SortedMap<Object, Object> parameters) {
    StringBuffer sb = new StringBuffer();
    sb.append("<xml>");
    Set<Entry<Object, Object>> es = parameters.entrySet();
    Iterator<Entry<Object, Object>> it = es.iterator();
    while (it.hasNext()) {
      Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
      String k = (String) entry.getKey();
      String v = (String) entry.getValue();
      if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k)
          || "sign".equalsIgnoreCase(k)) {
        sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
      } else {
        sb.append("<" + k + ">" + v + "</" + k + ">");
      }
    }
    sb.append("</xml>");
    return sb.toString();
  }

  /**
   * @author Joeson
   * @date 2014-12-3上午10:17:43
   * @Description：返回给微信的参数
   * @param return_code 返回编码
   * @param return_msg 返回信息
   * @return
   */
  public static String setXML(String return_code, String return_msg) {
    return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA["
        + return_msg + "]]></return_msg></xml>";
  }
}
